#include "talkmsglistitem.h"
#include "talkui.h"

#include <QHBoxLayout>
#include <QPainter>
#include <QSizePolicy>
TalkMsgListItem::TalkMsgListItem(bool isMe, QWidget *parent) : QWidget(parent)
{
    initUi(isMe);
}

void TalkMsgListItem::initUi(bool isMe)
{

    nickName=new QLabel(this);
    nickName->setFixedHeight(20);
    nickName->setAlignment(Qt::AlignRight);
    nickName->setStyleSheet("QLabel{ border:none;color:black;font:13pt;}");
    avatar=new QWidget(this);
    avatar->setFixedSize(30,30);
    avatar->installEventFilter(this);
    hostName=new QLabel(this);
    hostName->setStyleSheet("QLabel{ border:none;background: #ffffff;color:black;font:15pt;padding:6px 10px;}");

    hostName->setWordWrap(true);
    hostName->setAlignment(Qt::AlignLeft);
    hostName->setTextInteractionFlags(Qt::TextSelectableByMouse);
    hostName->setCursor(Qt::IBeamCursor);

    QHBoxLayout *layoutH = new QHBoxLayout;
    layoutH->setSpacing(0);
    layoutH->setContentsMargins(16,10,16,10);

    QWidget  *userContent= new QWidget();
    userContent->setStyleSheet("QWidget{ border:none;}");
    userContent->setFixedWidth(30);
    QVBoxLayout *layoutV1 = new QVBoxLayout;
    layoutV1->addWidget(avatar);
    layoutV1->setMargin(0);
    layoutV1->setSpacing(0);
    layoutV1->addSpacerItem(new QSpacerItem(20, 20,QSizePolicy::Expanding,QSizePolicy::Expanding));
    userContent->setLayout(layoutV1);
    layoutH->addWidget(userContent);


    QVBoxLayout *layoutV = new QVBoxLayout;
    layoutV->setMargin(0);
    layoutV->setSpacing(0);
    layoutV->setContentsMargins(9,0,9,0);
    layoutV->addWidget(nickName);

    QWidget  *msgContent= new QWidget();
    msgContent->setStyleSheet("QWidget{ border:none;}");
    QVBoxLayout *layoutV2 = new QVBoxLayout;

    layoutV2->setMargin(0);
    layoutV2->setSpacing(0);
    layoutV2->addWidget(hostName);

    msgContent->setLayout(layoutV2);
    layoutV->addWidget(msgContent);
    layoutH->addLayout(layoutV);
    this->setLayout(layoutH);
    if(isMe){
        this->setLayoutDirection(Qt::RightToLeft);

        return;
    }
        nickName->setAlignment(Qt::AlignLeft);
        this->setLayoutDirection(Qt::LeftToRight);
}
bool TalkMsgListItem::eventFilter(QObject *obj, QEvent *event)
{
    if(obj == avatar)
    {
        if(event->type() == QEvent::Paint)
        {
            QPainter painter(avatar);
            painter.drawPixmap(avatar->rect(), QPixmap(":/img/avatar1.png"));
        }
    }
    return QWidget::eventFilter(obj, event);
}
